package com.colorful.insuranceproject.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.bean.DeliveryBean;
import com.lovely3x.common.adapter.BaseViewHolder;
import com.lovely3x.common.adapter.ListAdapter;

import java.util.List;

import butterknife.Bind;

/**
 * Created by handsome-Bob on 2017/8/21.
 * <p>
 * 收货地址的适配器
 */
public class DeliveryAdapter extends ListAdapter<DeliveryBean> {

    public DeliveryAdapter(List<DeliveryBean> datas, Context context) {
        super(datas, context);
    }

    @NonNull
    @Override
    public BaseViewHolder createViewHolder(int position, ViewGroup parent) {
        return new ViewHolder(getLayoutInflater().inflate(R.layout.item_delivery, parent, false));
    }

    @Override
    public void handleData(final int position, @NonNull BaseViewHolder holder) {
        ViewHolder viewHolder = (ViewHolder) holder;
        DeliveryBean bean = datas.get(position);

        viewHolder.tvNameAndPhone.setText(mContext.getString(R.string.name_and_phone, bean.getUserName(), bean.getUserPhone()));
        if (bean.getIsDefault().equals("1")) {
            viewHolder.tvDefault.setText(mContext.getString(R.string.isDefault));
            viewHolder.tvDefault.setTextColor(ContextCompat.getColor(mContext, R.color.red_ff5a54));
        } else {
            viewHolder.tvDefault.setText(mContext.getString(R.string.setDefault));
            viewHolder.tvDefault.setTextColor(ContextCompat.getColor(mContext, R.color.auxiliary_text_bg));
        }

        viewHolder.viewId.setVisibility(datas.size() <= 1 ? View.GONE : View.VISIBLE);

        viewHolder.tvAddress.setText(bean.getUserAddress());

        final View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mViewClickedListener != null) {
                    mViewClickedListener.onViewClicked(position, v);
                }
            }
        };

        viewHolder.tvDefault.setOnClickListener(clickListener);
        viewHolder.imgOne.setOnClickListener(clickListener);
        viewHolder.imgTwo.setOnClickListener(clickListener);
    }

    class ViewHolder extends BaseViewHolder {

        @Bind(R.id.tv_name_and_phone)
        TextView tvNameAndPhone;

        @Bind(R.id.tv_default)
        TextView tvDefault;

        @Bind(R.id.tv_address)
        TextView tvAddress;

        @Bind(R.id.img_one)
        ImageView imgOne;

        @Bind(R.id.img_two)
        ImageView imgTwo;

        @Bind(R.id.viewId)
        View viewId;

        public ViewHolder(View rootView) {
            super(rootView);
        }
    }
}
